package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.bean.message.template.TemplateDate;
import com.xmy.cultivate.bean.message.template.TemplateMessage;
import com.xmy.cultivate.entity.WecharTemplate;
import com.xmy.cultivate.entity.WecharsBind;
import com.xmy.cultivate.mapper.WecharTemplateMapper;
import com.xmy.cultivate.mapper.WecharsBindMapper;
import com.xmy.cultivate.service.IMqPushService;
import com.xmy.cultivate.service.IWecharsBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 微信绑定 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class WecharsBindServiceImpl extends ServiceImpl<WecharsBindMapper, WecharsBind> implements IWecharsBindService {

    @Autowired
    WecharsBindMapper wecharsBindMapper;

    @Autowired
    WecharTemplateMapper wecharTemplateMapper;

    @Autowired
    IMqPushService iMqPushService;



    @Override
    public List<String> getSendOpenid(Long studentId){
        return wecharsBindMapper.getSendOpenid(studentId);
    }

    /**
     * 上课提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxPushOnCourse(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",1);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    //String ss =  wecharFeignClient.sendWecharMsg(obj);
                    iMqPushService.pushWechart(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 使用课时提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxPushUseCourseHouse(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String keyword4Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",3);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");

                    HashMap<String,String> keyword3 = new HashMap<>();
                    keyword3.put("value",keyword3Param);
                    keyword3.put("color","#33A1C9");

                    HashMap<String,String> keyword4 = new HashMap<>();
                    keyword4.put("value",keyword4Param);
                    keyword4.put("color","#33A1C9");

                    /*HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");*/
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).keyword3(keyword3).keyword4(keyword4).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.pushWechart(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 到离校提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxPushOnOffSchool(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",2);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");

                    HashMap<String,String> keyword3 = new HashMap<>();
                    keyword3.put("value",keyword3Param);
                    keyword3.put("color","#33A1C9");

                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).keyword3(keyword3).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.pushWechart(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 上课提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxCalOnCourse(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",8);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.pushWechart(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }

    /**
     * 补课上课前提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param keyword3Param
     * @param keyword4Param
     * @param keyword5Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxPushMakeUpMissedLessons(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String keyword4Param, String keyword5Param, String remarkParam, String clientMsgId){
        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",4);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> keyword3 = new HashMap<>();
                    keyword3.put("value",keyword3Param);
                    keyword3.put("color","#33A1C9");
                    HashMap<String,String> keyword4 = new HashMap<>();
                    keyword4.put("value",keyword4Param);
                    keyword4.put("color","#33A1C9");
                    HashMap<String,String> keyword5 = new HashMap<>();
                    keyword5.put("value",keyword5Param);
                    keyword5.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).keyword3(keyword3).keyword4(keyword4).keyword5(keyword5).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();
                    String obj = templateMessage.toJson();
                    iMqPushService.pushWechart(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 缺课提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxPushMissAClass(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String remarkParam, String clientMsgId){
        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",5);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> keyword3 = new HashMap<>();
                    keyword3.put("value",keyword3Param);
                    keyword3.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).keyword3(keyword3).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.pushWechart(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }

}
